//
//  UIView+ZYBCustomToast.m
//  homework
//
//  Created by AW Technician on 16/1/6.
//  Copyright © 2016年 zyb. All rights reserved.
//

#import "UIView+ZYBCustomToast.h"
#import <objc/runtime.h>
#import "ZYBToastView.h"
#import "UIView+NativeToast.h"
#import "ZYBUIDefine.h"
#import "NSArray+Safety.h"

static  const NSString * CSToastViewKey   = @"CSToastViewKey";
static  const NSString * CSLoadingToastViewKey   = @"CSLoadingToastViewKey";
static  const NSString * CSCenterPoint = @"CSCenterPoint";
static  const NSString * CSComplete = @"CSComplete";

static  NSString * kWarning          = @"tost_warning.png";
static  NSString * kSuccess          = @"tost_succeed.png";
static  NSString * kNetError         = @"tost_netError.png";

@implementation UIView (ZYBCustomToast)
- (void)hidePreToastWithKey:(const void *)key
{
    UIView *preToast = objc_getAssociatedObject(self, key);
    if (preToast) {
        preToast.hidden = YES;
        [preToast removeFromSuperview];
        preToast = nil;
    }
}

- (ZYBToastView *)tostViewWithImageName:(NSString *)imageName message:(NSString *)message
{
    ZYBToastView *toast = [[ZYBToastView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    [toast.customImageView setImage:[UIImage imageNamed:imageName]];
    [toast.label1 setText:message];
    objc_setAssociatedObject (self, &CSToastViewKey, toast, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    return toast;
}

- (ZYBToastView *)loadingTostViewMessage:(NSString *)message isHorizontalScreen:(BOOL)isHorizontalScreen
{
    CGRect frame;
    if (isHorizontalScreen) {
        frame = CGRectMake(0, 0, kScreenHeight, kScreenWidth);
    } else {
        frame = [UIScreen mainScreen].bounds;
    }
    ZYBToastView *toast = [[ZYBToastView alloc] initWithFrame:frame];
    UIActivityIndicatorView *customView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    toast.customViewOffSet = CGPointMake(0.0f, 40.0f);
    
    [customView startAnimating];
    [toast setCustomView:customView];
    [toast.label1 setText:message];
    objc_setAssociatedObject (self, &CSLoadingToastViewKey, toast, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    return toast;
}


- (ZYBToastView *)loadingTostViewAboutBrandLogo
{
    ZYBToastView *toast = [[ZYBToastView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    toast.blackAlpha = 0.0f;
    
    UIImageView *loadingImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 150, 150)];// 75, 0, 150, 150
    loadingImageView.contentMode = UIViewContentModeScaleAspectFill;
    
    NSMutableArray *images = [NSMutableArray array];
    for (int i = 0; i < 52; i++) {
        [images addObjectSafely:[UIImage imageNamed:[NSString stringWithFormat:@"列表刷新%d",i+1]]];
    }
    loadingImageView.animationImages = images;
    loadingImageView.animationDuration = 2.6;
    
    toast.customViewOffSet = CGPointMake(0.0f, 0.0f);
    [loadingImageView startAnimating];
    [toast setCustomView:loadingImageView];
    objc_setAssociatedObject (self, &CSLoadingToastViewKey, toast, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    return toast;
}



- (void)showToast:(UIView *)toast duration:(NSTimeInterval)duration
{
    if (CGPointEqualToPoint(self.toastCenterPoint, CGPointZero))
    {
        [self showToast:toast
               duration:duration
               position:CSToastPositionCenter];
    }
    else
    {
        [self showToast:toast
               duration:duration
               position:[NSValue valueWithCGPoint:self.toastCenterPoint]];
    }
}

- (void)showToastWithImageName:(NSString *)imageName message:(NSString *)message
{
    [self hidePreToastWithKey:&CSToastViewKey];
    ZYBToastView *toast = [self tostViewWithImageName:imageName message:message];
    [self showToast:toast
           duration:1];
}

- (void)showErrorMessage:(NSString *)aMessage
{
    [self showToastWithImageName:kWarning message:aMessage];
}

- (void)showErrorMessage:(NSString *)aMessage time:(CGFloat)time
{
    [self showToastLongTimeWithImageName:kWarning message:aMessage time:time];
}

- (void)showToastLongTimeWithImageName:(NSString *)imageName message:(NSString *)message time:(CGFloat)time
{
    [self hidePreToastWithKey:&CSLoadingToastViewKey];
    ZYBToastView *toast = [self tostViewWithImageName:imageName message:message];
    [self showToast:toast
           duration:time];
}

- (void)showSucceedMessage:(NSString *)aMessage
{
    [self showSucceedMessage:aMessage complete:nil];
}

- (void)showNetErrorWithMessage:(NSString *)aMessage
{
    [self showNetErrorWithMessage:aMessage time:0.5];
//    [self showToastWithImageName:kNetError message:aMessage];
}

- (void)showNetErrorWithMessage:(NSString *)aMessage time:(CGFloat)time {
    
    [self hidePreToastWithKey:&CSLoadingToastViewKey];
    ZYBToastView *toast = [self tostViewWithImageName:kNetError message:aMessage];
    [self showToast:toast
           duration:time];
}

- (void)showLoadingMessage:(NSString *)aMessage
{
    [self removeLoadingTost];
    ZYBToastView *toast = [self loadingTostViewMessage:aMessage isHorizontalScreen:NO];
    [self showToast:toast
           duration:MAXFLOAT];
}

- (void)showLoadingMessage:(NSString *)aMessage isHorizontalScreen:(BOOL)isHorizontalScreen {
    if (!isHorizontalScreen) {
        [self showLoadingMessage:aMessage];
    } else {
        [self removeLoadingTost];
        ZYBToastView *toast = [self loadingTostViewMessage:aMessage isHorizontalScreen:isHorizontalScreen];
        [self showToast:toast
               duration:MAXFLOAT];
    }
    
}

- (void)showLoadingBrandLogoAnimation  // 小熊猫品牌loading动画
{
    [self removeLoadingTost];
    
    ZYBToastView *toast = [self loadingTostViewAboutBrandLogo];
    [self showToast:toast duration:MAXFLOAT];
    
}

- (void)showLoadingMessageWithCurrentLoadingToast:(NSString *)aMessage
{
    ZYBToastView *preToast = objc_getAssociatedObject(self, &CSLoadingToastViewKey);
    if (preToast) {
        [preToast.label1 setText:aMessage];
        CGRect frame = preToast.label1.frame;
        frame.size.width = preToast.frame.size.width;
        preToast.label1.frame = frame;
        preToast.label1.center = CGPointMake(preToast.frame.size.width*.5f, preToast.label1.center.y);
        [preToast setNeedsDisplay];
    }else{
        ZYBToastView *toast = [self loadingTostViewMessage:aMessage isHorizontalScreen:NO];
        [self showToast:toast
               duration:MAXFLOAT];
    }
}

- (void)showSucceedMessage:(NSString *)aMessage complete:(ShowComplete)complete
{
    self.showComplete = complete;
    
    [self showToastWithImageName:kSuccess message:aMessage];
}

- (void)showErrorMessage:(NSString *)aMessage complete:(ShowComplete)complete {
    self.showComplete = complete;
    [self showToastWithImageName:kWarning message:aMessage];
}

- (void)removeLoadingTost
{
    [self hidePreToastWithKey:&CSLoadingToastViewKey];
}

- (void)setToastCenterPoint:(CGPoint)value
{
    [self willChangeValueForKey:@"toastCenterPoint"];
    objc_setAssociatedObject(self, &CSCenterPoint, [NSValue valueWithCGPoint:value], OBJC_ASSOCIATION_RETAIN);
    [self didChangeValueForKey:@"toastCenterPoint"];
}

- (CGPoint)toastCenterPoint
{
    NSValue *value = objc_getAssociatedObject(self, &CSCenterPoint);
    return [value CGPointValue];
}

- (void)setShowComplete:(ShowComplete)showComplete
{
    [self willChangeValueForKey:@"showComplete"];
    objc_setAssociatedObject(self, &CSComplete, showComplete, OBJC_ASSOCIATION_COPY);
    [self didChangeValueForKey:@"showComplete"];
}

- (ShowComplete)showComplete
{
    return objc_getAssociatedObject(self, &CSComplete);
}

@end
